/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package janelas;


import Classes.*;
import java.util.*;
import javax.swing.AbstractListModel;

/**
 *
 * @author Daniela
 */
public class PesquisarLivro extends javax.swing.JFrame {

    private ContaDoUsuario janelaAnterior;
    public PesquisarLivro(ContaDoUsuario janelaAnterior) {
        this.janelaAnterior = janelaAnterior;
        initComponents();
        preencheLivros();
        setLocationRelativeTo(null);
        setResizable(false);
    }

    private void preencheLivros() { 
        List<String> listaNomes = new ArrayList<>();
        for (Usuario u : janelaAnterior.getSistema().getLista()){
            for (Item i : u.getEstante().getLista()) {
                listaNomes.add(i.getNome());
            }
        }
        preencheLista(listaNomes);
    }
    
    private void preencheLista(final List<String> lista) {
        listaLivros.setModel(new AbstractListModel(){
            @Override
            public int getSize() { return lista.size(); }
            @Override
            public Object getElementAt(int indice) { return lista.get(indice); }
        });
    }
    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        campoPesquisar = new javax.swing.JTextField();
        jScrollPane1 = new javax.swing.JScrollPane();
        listaLivros = new javax.swing.JList();
        labelPesquisar = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Pesquisando Item");
        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowClosed(java.awt.event.WindowEvent evt) {
                formWindowClosed(evt);
            }
        });

        jPanel1.setBackground(new java.awt.Color(204, 204, 255));

        campoPesquisar.addCaretListener(new javax.swing.event.CaretListener() {
            public void caretUpdate(javax.swing.event.CaretEvent evt) {
                campoPesquisarCaretUpdate(evt);
            }
        });

        listaLivros.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                listaLivrosMouseClicked(evt);
            }
        });
        jScrollPane1.setViewportView(listaLivros);

        labelPesquisar.setIcon(new javax.swing.ImageIcon("C:\\Users\\Daniela\\Desktop\\Icons\\buscar.gif")); // NOI18N
        labelPesquisar.setText("Pesquisar:");

        jLabel2.setFont(new java.awt.Font("Tahoma", 1, 18)); // NOI18N
        jLabel2.setText("Você está pesquisando um livro");

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGap(53, 53, 53)
                        .addComponent(jLabel2))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGap(32, 32, 32)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                            .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 327, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addGroup(javax.swing.GroupLayout.Alignment.LEADING, jPanel1Layout.createSequentialGroup()
                                .addComponent(labelPesquisar)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(campoPesquisar, javax.swing.GroupLayout.PREFERRED_SIZE, 253, javax.swing.GroupLayout.PREFERRED_SIZE)))))
                .addContainerGap(31, Short.MAX_VALUE))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGap(27, 27, 27)
                .addComponent(jLabel2)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGap(4, 4, 4)
                        .addComponent(labelPesquisar))
                    .addComponent(campoPesquisar, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 193, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(52, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void getListaChave(String chave) {
        List<String> listaNomes = new ArrayList<>();
        for (Usuario u : janelaAnterior.getSistema().getLista()){
            for (Item i : u.getEstante().getLista()) {
                if (i.getNome().contains(chave))
                    listaNomes.add(i.getNome());
            }
        }
        preencheLista(listaNomes);
    }
    
    private void campoPesquisarCaretUpdate(javax.swing.event.CaretEvent evt) {//GEN-FIRST:event_campoPesquisarCaretUpdate
        getListaChave(campoPesquisar.getText());
    }//GEN-LAST:event_campoPesquisarCaretUpdate

    private void listaLivrosMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_listaLivrosMouseClicked
        String nomeItem = (String)listaLivros.getSelectedValue();
        if (nomeItem != null) {
            Item item = null;
            for (Usuario u : janelaAnterior.getSistema().getLista()){
                for (Item i : u.getEstante().getLista()) {
                    if (i.getNome().equals(nomeItem))
                            item = i;
                }
            }
            new JanelaLivro(janelaAnterior, item).setVisible(true);
            this.dispose();
        }
    }//GEN-LAST:event_listaLivrosMouseClicked

    private void formWindowClosed(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_formWindowClosed
        janelaAnterior.setVisible(true);
        janelaAnterior.setEnabled(true);

    }//GEN-LAST:event_formWindowClosed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JTextField campoPesquisar;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JLabel labelPesquisar;
    private javax.swing.JList listaLivros;
    // End of variables declaration//GEN-END:variables
}
